package com.atguigu.tingshu.album.task;

import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.atguigu.tingshu.common.constant.SystemConstant.*;

@Slf4j
@Component
public class ReviceResultTask {


    @Autowired
    private TrackInfoMapper trackInfoMapper;
    @Autowired
    private AuditService auditService;

    //定时获取审核结果
//    @Scheduled(cron = "0/5 * * * * ?")
    public void reviceResultJob(){
        log.info("开始获取审核结果");
        //根据条件: 1.审核中结果 2.限制数量 3.查询声音ID跟审核任务ID
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(new LambdaQueryWrapper<TrackInfo>()
                .eq(TrackInfo::getStatus, TRACK_STATUS_REVIEWING)
                .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                .orderByAsc(TrackInfo::getId)
                .last("limit 100"));

if(ObjectUtils.isNotEmpty(trackInfoList)){
    //遍历声音列表
    for (TrackInfo trackInfo : trackInfoList) {
        //根据声音表中审核任务ID查询审核结果
        String suggestion = auditService.getReviewTaskResult(trackInfo.getReviewTaskId());
      if(StringUtils.isNotBlank(suggestion)){
          //根据审核建议更新审核状态
          if("pass".equals(suggestion)){
              trackInfo.setStatus(TRACK_STATUS_PASS);
          } else if ("review".equals(suggestion)) {
              trackInfo.setStatus(TRACK_STATUS_REVIEWING);
          } else if ("block".equals(suggestion)) {
              trackInfo.setStatus(TRACK_STATUS_NO_PASS);
          }
          trackInfoMapper.updateById(trackInfo);
      }
    }
}

    }

}
